package com.jjtm.common.core.utils;

import com.jjtm.common.core.constants.ErrorConstants;
import com.jjtm.common.core.expections.BussinessException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author wangbaofeng
 *
 */
@Component
public class FileUtils {

	static Logger logger = LoggerFactory.getLogger(new FileUtils().getClass());

	// 每片最少5MB，这里写死了，最好用配置文件随时修改
	// final static long PART_SIZE = 5 * 1024 * 1024L;

	public String putFile(MultipartFile file, String accessKey, String secretKey, String bucketName) {
		// 上传到七牛后保存的文件名
		String key = "";
		String jpg = ".jpg";
		String png = ".png";
		if (file.getOriginalFilename().contains(jpg) || file.getOriginalFilename().contains(png)) {
			// 七牛云服务器里用来对应唯一上传的文件
			key = OrderNoUtils.getSerialNumber()
					+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		} else {
			key = OrderNoUtils.getSerialNumber() + ".png";
		}

		// 密钥配置
		Auth auth = Auth.create(accessKey, secretKey);

		//构造一个带指定Region对象的配置类
		Configuration cfg = new Configuration(Region.autoRegion());

		// 创建上传对象
		UploadManager uploadManager = new UploadManager(cfg);
		try {
			String fileName = file.getOriginalFilename();
			if (fileName.trim() != "") {
				// 调用put方法上传 Response res =
				Response res = uploadManager.put(file.getBytes(), key, auth.uploadToken(bucketName));
				if (!res.isOK()) {
					return res.bodyString();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return key;
	}

	public List<String> putFileList(List<MultipartFile> files, String accessKey, String secretKey, String bucketName) {

		if (files.isEmpty()) {
			throw new BussinessException("请添加图片");
		}

		// 文件存到服务器的KEY
		List<String> keys = new ArrayList<>();
		try {
			if (files.size() != 0) {
				for (MultipartFile file : files) {
					// 上传到七牛后保存的文件名
					System.err.println(file.getOriginalFilename());
					// 七牛云服务器里用来对应唯一上传的文件
					String key = OrderNoUtils.getSerialNumber()
							+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
					// 密钥配置
					Auth auth = Auth.create(accessKey, secretKey);
					//构造一个带指定Region对象的配置类
					Configuration cfg = new Configuration(Region.autoRegion());

					// 创建上传对象
					UploadManager uploadManager = new UploadManager(cfg);

					String fileName = file.getOriginalFilename();
					if (fileName.trim() != "") {
						// 调用put方法上传 Response res =
						Response res = uploadManager.put(file.getBytes(), key, auth.uploadToken(bucketName));
						if (!res.isOK()) {
							throw new BussinessException(res.bodyString());
						}
						keys.add(key);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return keys;
	}

	public String putLocalFile(File file, String accessKey, String secretKey, String bucketName) {
		if (file == null) {
			throw new BussinessException(ErrorConstants.BUG_ERR, "上传文件为空");
		}

		// 获取文件名称
		String fileName = file.getName();
		// 上传到七牛后保存的文件名
		// 七牛云服务器里用来对应唯一上传的文件
		String key = OrderNoUtils.getSerialNumber() + fileName.substring(fileName.lastIndexOf("."));
		// 密钥配置
		Auth auth = Auth.create(accessKey, secretKey);

		//构造一个带指定Region对象的配置类
		Configuration cfg = new Configuration(Region.autoRegion());

		// 创建上传对象
		UploadManager uploadManager = new UploadManager(cfg);

		// 上传并获取进度
		try {
			if (fileName.trim() != "") {
				// 调用put方法上传
				Response res = uploadManager.put(file, key, auth.uploadToken(bucketName));
				if (!res.isOK()) {
					return res.bodyString();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return key;
	}

}
